package com.czkevin.cif.enchantments;

import java.util.*;

public final class EnchantmentRegister {

  private EnchantmentRegister() { }

  private static final Map<String, Enchantment> types = new HashMap<>();
  private static final Map<Class<? extends Enchantment>, Enchantment> typeInstances = new HashMap<>();

  /**
   * 注册附魔
   * @param enchantment Enchantment to register.
   */
  public static void register(Enchantment enchantment) {
    if(enchantment == null || enchantment.key() == null) {
      throw new IllegalArgumentException("invalid enchantment");
    }
    if(types.containsKey(enchantment.key())) {
      throw new IllegalArgumentException("enchantment key exists");
    }
    if(typeInstances.containsKey(enchantment.getClass())) {
      throw new IllegalArgumentException("enchantment class entry exists");
    }
    types.put(enchantment.key(), enchantment);
    typeInstances.put(enchantment.getClass(), enchantment);
  }

  public static Enchantment get(String type) {
    return types.get(type);
  }

  public static Enchantment get(Class<? extends Enchantment> type) {
    return typeInstances.get(type);
  }

  public static List<String> listTypes() {
    return Collections.unmodifiableList(new ArrayList<>(types.keySet()));
  }

}
